var year;
var month;
var identify;
var vatName;
var vatAddress;
var vatOrg;
var thisAllowCredit;
var preLeftCredit;
var totalAllowCredit;
var applyVatCredit;
var thisLeftCredit;

var sepInvoiceWidgets = [];
var manInvoiceWidgets = [];
var fixInvoiceWidgets = [];
var othInvoiceWidgets = [];

// 创建基础控件 */
function createBasicWidget(credit)
{
	var now = new Date();
	
	year = $$("select");
	year.onchange = left;
	var y = credit == null ? now.getFullYear() : credit.year;
	for (var i = 0; i < 10; i++)
	{
		var opt = $$("option");
		opt.value = y + i - 4;
		opt.innerHTML = opt.value + " 年";
		if (opt.value == y) opt.selected = true;
			
		year.appendChild(opt);
	}
	
	month = $$("select");
	month.onchange = left;
	var m = credit == null ? now.getMonth() + 1 : credit.month;
	for (var i = 0; i < 12; i++)
	{
		var opt = $$("option");
		opt.value = i + 1;
		opt.innerHTML = opt.value + " 月";
		if (opt.value == m) opt.selected = true;
			
		month.appendChild(opt);
	}
	
	identify = $$("input");
	identify.className = "text_input200";
	identify.onkeyup = suggest;
	if (credit != null) identify.value = credit.identify;
	
	vatName = $$("input");
	vatName.className = "text_input200";
	vatName.readOnly = true;
	if (credit != null && credit.user != null) vatName.value = credit.user.name;
	
	vatAddress = $$("input");
	vatAddress.className = "text_input200";
	vatAddress.readOnly = true;
	if (credit != null && credit.user != null) vatAddress.value = credit.user.address;
	
	vatOrg = $$("input");
	vatOrg.className = "text_input200";
	vatOrg.readOnly = true;
	if (credit != null && credit.user != null && credit.user.org != null) vatOrg.value = credit.user.org.name;
	
	//注，以下5个地方的值，仅设置applyVatCredit的，其它的将在后面创建完整个table后，通过调用 left() 自动计算
	
	thisAllowCredit = $$("input");
	thisAllowCredit.className = "text_input120";
	thisAllowCredit.readOnly = true;
	thisAllowCredit.value = "0";
	
	preLeftCredit = $$("input");
	preLeftCredit.className = "text_input120";
	preLeftCredit.readOnly = true;
	preLeftCredit.value = "0";
	
	totalAllowCredit = $$("input");
	totalAllowCredit.className = "text_input120";
	totalAllowCredit.readOnly = true;
	totalAllowCredit.value = "0";
	
	applyVatCredit = $$("input");
	applyVatCredit.className = "text_input340";
	if (credit != null) applyVatCredit.value = credit.applyVatCredit;
	applyVatCredit.onchange = function() { check(applyVatCredit) };
	
	thisLeftCredit = $$("input");
	thisLeftCredit.className = "text_input120";
	thisLeftCredit.readOnly = true;
	thisLeftCredit.value = "0";
}

//创建项目内容选择器 */
function createVatTypeSelect()
{
	var select = $$("select");
	select.className = "text_input120";
	var opt = $$("option");
	opt.value = -1;
	opt.innerHTML = "请选择...";
	select.appendChild(opt);
	for (var i = 0; i < types.length; i++)
	{
		var opt = $$("option");
		opt.value = types[i].id;
		opt.innerHTML = types[i].name;
		select.appendChild(opt);
	}
	return select;
}

//创建填写发票的控件组
function createInvoiceWidgets(invoice, first, all)
{
	var widgets = {};
	
	if (all)
	{
		var invoiceCode = $$("input");
		invoiceCode.className = "text_input120";
		if (invoice != null) invoiceCode.value = invoice.invoiceCode;
		else invoiceCode.disabled = true;
		widgets.code = invoiceCode;
		
		var invoiceNum = $$("input");
		invoiceNum.className = "text_input120";
		if (invoice != null) invoiceNum.value = invoice.invoiceNum;
		else invoiceNum.disabled = true;
		widgets.num = invoiceNum;
	}

	var invoiceType = createVatTypeSelect();
	if (invoice != null) invoiceType.value = invoice.invoiceTypeId;
	widgets.type = invoiceType;
	invoiceType.onchange = function()
	{
		if (widgets.code) widgets.code.disabled = invoiceType.value < 0;
		if (widgets.num) widgets.num.disabled = invoiceType.value < 0;
		widgets.money.disabled = invoiceType.value < 0;
		widgets.date.disabled = invoiceType.value < 0;
		
		if (invoiceType.value < 0)
		{
			if (widgets.code) widgets.code.value = "";
			if (widgets.num) widgets.num.value = "";
			widgets.money.value = "";
			widgets.date.value = "";
			
			money();
		}
	}
	
	var invoiceMoney = $$("input");
	invoiceMoney.className = "text_input75";
	if (invoice != null) invoiceMoney.value = invoice.invoiceMoney;
	else invoiceMoney.disabled = true;
	invoiceMoney.onchange = function() { check(invoiceMoney) };
	widgets.money = invoiceMoney;
	
	var invoiceDate = $$("input");
	invoiceDate.className = "text_input75";
	if (invoice != null) invoiceDate.value = dateToString(invoice.invoiceDate);
	else invoiceDate.disabled = true;
	widgets.date = invoiceDate;
	
	var addButton = $$("img");
	addButton.style.cursor = "pointer";
	addButton.src = first ? "images/main/add.gif" : "images/main/delete.gif";
	widgets.button = addButton;
	
	return widgets;
}

// 创建所期选择器
function createInnerTable()
{
	var list = [];
	var line = [{text:"所属期："}, {widget:year}, {widget:month}];
	
	list.push(line);
	var table = createTable(list);
	table.className = "inner_table";
	table.border = "0";
	table.cellPadding = "0";
	table.cellSpacing = "0";
	table.align = "center";

	return table;
}

//创建增值税专用发票控件组
function createSepInvoice(table, sepInvoices)
{
	var tr = addTableRow(table, 7);
	var sepInvoiceCell = addTableCell(tr, [{text:"增值税专用发票", col:2}]);
	
	var first = null;
	if (sepInvoices.length > 0) first = sepInvoices[0];
	
	var ws = createInvoiceWidgets(first, true, true);
	sepInvoiceWidgets.push(ws);
	
	var createSepInvoicePlus = function(invoice)
	{
		var xx = createInvoiceWidgets(invoice, false, true);
		var xtr = addTableRow(table, 7 + sepInvoiceWidgets.length);
		sepInvoiceWidgets.push(xx);
		
		sepInvoiceCell.rowSpan = sepInvoiceWidgets.length;
		addTableCell(xtr, [{widget:xx.code}, {widget:xx.num}, {widget:xx.type}, {widget:xx.money}, {widget:xx.date}, {widget:xx.button}]);
	
		xx.button.title = "删除";
		xx.button.onclick = function()
		{
			table.deleteRow(xtr.rowIndex);
			remove(sepInvoiceWidgets, xx);
			sepInvoiceCell.rowSpan = sepInvoiceWidgets.length;
			
			money();
		}
	}
	
	ws.button.title = "继续添加增值税专用发票";
	ws.button.onclick = function()
	{
		createSepInvoicePlus(null);
	}
	
	addTableCell(tr, [{widget:ws.code}, {widget:ws.num}, {widget:ws.type}, {widget:ws.money}, {widget:ws.date}, {widget:ws.button}]);

	for (var i = 1; i < sepInvoices.length; i++)
	{
		createSepInvoicePlus(sepInvoices[i]);
	}
}

//创建普通发票控件组
function createNorInvoice(table, manInvoices, fixInvoices)
{
	var tr = addTableRow(table, 7 + sepInvoiceWidgets.length);
	var norInvoiceCell = addTableCell(tr, [{text:"普通发票",row:2}]);
	
	var manFirst = null;
	if (manInvoices.length > 0) manFirst = manInvoices[0];
	
	var manInvoiceCell = addTableCell(tr, [{text:"通用手工发票"}]);
	var manWs = createInvoiceWidgets(manFirst, true, true);
	manInvoiceWidgets.push(manWs);
	
	var createManInvoicePlus = function(invoice)
	{
		var xx1 = createInvoiceWidgets(invoice, false, true);
		var xtr1 = addTableRow(table, 7 + sepInvoiceWidgets.length + manInvoiceWidgets.length);
		manInvoiceWidgets.push(xx1);
		
		manInvoiceCell.rowSpan = manInvoiceWidgets.length;
		norInvoiceCell.rowSpan = manInvoiceWidgets.length + fixInvoiceWidgets.length;
		
		addTableCell(xtr1, [{widget:xx1.code}, {widget:xx1.num}, {widget:xx1.type}, {widget:xx1.money}, {widget:xx1.date}, {widget:xx1.button}]);
		xx1.button.title = "删除";
		xx1.button.onclick = function()
		{
			table.deleteRow(xtr1.rowIndex);
			remove(manInvoiceWidgets, xx1);
			
			manInvoiceCell.rowSpan = manInvoiceWidgets.length;
			norInvoiceCell.rowSpan = manInvoiceWidgets.length + fixInvoiceWidgets.length;
			
			money();
		}
	}
	
	manWs.button.title = "继续添加通用手工发票";
	manWs.button.onclick = function()
	{
		createManInvoicePlus(null);
	}
	addTableCell(tr, [{widget:manWs.code}, {widget:manWs.num}, {widget:manWs.type}, {widget:manWs.money}, {widget:manWs.date}, {widget:manWs.button}]);

	for (var i = 1; i < manInvoices.length; i++)
	{
		createManInvoicePlus(manInvoices[i]);
	}
	
	tr = addTableRow(table, 7 + sepInvoiceWidgets.length + manInvoiceWidgets.length);
	
	var fixFirst = null;
	if (fixInvoices.length > 0) fixFirst = fixInvoices[0];
	
	var fixInvoiceCell = addTableCell(tr, [{text:"定额发票"}]);
	var fixWs = createInvoiceWidgets(fixFirst, true, true);
	
	var createFixInvoicePlus = function(invoice)
	{
		var xx2 = createInvoiceWidgets(invoice, false, true);
		var xtr2 = addTableRow(table, 7 + sepInvoiceWidgets.length + manInvoiceWidgets.length + fixInvoiceWidgets.length);
		fixInvoiceWidgets.push(xx2);
		
		fixInvoiceCell.rowSpan = fixInvoiceWidgets.length;
		norInvoiceCell.rowSpan = manInvoiceWidgets.length + fixInvoiceWidgets.length;
		
		addTableCell(xtr2, [{widget:xx2.code}, {widget:xx2.num}, {widget:xx2.type}, {widget:xx2.money}, {widget:xx2.date}, {widget:xx2.button}]);
		xx2.button.title = "删除";
		xx2.button.onclick = function()
		{
			table.deleteRow(xtr2.rowIndex);
			remove(fixInvoiceWidgets, xx2);
			
			fixInvoiceCell.rowSpan = fixInvoiceWidgets.length;
			norInvoiceCell.rowSpan = manInvoiceWidgets.length + fixInvoiceWidgets.length;
			
			money();
		}
	}
	
	fixInvoiceWidgets.push(fixWs);
	fixWs.button.title = "继续添加定额发票";
	fixWs.button.onclick = function()
	{
		createFixInvoicePlus(null);
	}
	addTableCell(tr, [{widget:fixWs.code}, {widget:fixWs.num}, {widget:fixWs.type}, {widget:fixWs.money}, {widget:fixWs.date}, {widget:fixWs.button}]);

	for (var i = 1; i < fixInvoices.length; i++)
	{
		createFixInvoicePlus(fixInvoices[i]);
	}
}

//创建其它发票控件组
function createOthInvoice(table, othInvoices)
{
	var tr = addTableRow(table, 7 + sepInvoiceWidgets.length + manInvoiceWidgets.length + fixInvoiceWidgets.length);
	var othInvoiceCell = addTableCell(tr, [{text:"其它",col:2}]);
	
	var first = null;
	if (othInvoices.length > 0) first = othInvoices[0];
	
	var ws = createInvoiceWidgets(first, true, false);
	othInvoiceWidgets.push(ws);
	
	function createOthInvoicePlus(invoice)
	{
		var xx = createInvoiceWidgets(invoice, false, false);
		var xtr = addTableRow(table, 7 + sepInvoiceWidgets.length + manInvoiceWidgets.length + fixInvoiceWidgets.length + othInvoiceWidgets.length);
		othInvoiceWidgets.push(xx);
		
		othInvoiceCell.rowSpan = othInvoiceWidgets.length;
		addTableCell(xtr, [{text:"-"}, {text:"-"}, {widget:xx.type}, {widget:xx.money}, {widget:xx.date}, {widget:xx.button}]);
	
		xx.button.title = "删除";
		xx.button.onclick = function()
		{
			table.deleteRow(xtr.rowIndex);
			remove(othInvoiceWidgets, xx);
			othInvoiceCell.rowSpan = othInvoiceWidgets.length;
			
			money();
		}
	}
	
	ws.button.title = "继续添加其它发票";
	ws.button.onclick = function()
	{
		createOthInvoicePlus(null);
	}
	addTableCell(tr, [{text:"-"}, {text:"-"}, {widget:ws.type}, {widget:ws.money}, {widget:ws.date}, {widget:ws.button}]);

	for (var i = 1; i < othInvoices.length; i++)
	{
		createOthInvoicePlus(othInvoices[i]);
	}
}

//创建页面表格
function createSubmitTable(credit)
{
	createBasicWidget(credit);
	var inner = createInnerTable();
	
	var list = [];
	var line;
	
	var submitTime = credit == null ? new Date() : credit.submitTime;
	var szSubmitTime = dateToLongString(submitTime);
	
	line = [{text:"日期信息",col:8,style:"title"}];
	list.push(line);
	line = [{text:"填开日期："+szSubmitTime,col:2}, {widget:inner,col:3}, {text:"金额单位：元",col:3}];
	list.push(line);
	line = [{text:"纳税人信息",col:8,style:"title"}];
	list.push(line);
	line = [{text:"纳税人识别号",col:2},{widget:identify,col:2},{text:"纳税人名称"},{widget:vatName,col:3}];
	list.push(line);
	line = [{text:"纳税人地址",col:2},{widget:vatAddress,col:2},{text:"主管国家机关"},{widget:vatOrg,col:3}];
	list.push(line);
	line = [{text:"增值税应纳税额抵减凭证信息",col:8,style:"title"}];
	list.push(line);
	line = [{text:"抵减凭证类别",col:2},{text:"发票代码"},{text:"发票号码"},{text:"项目内容"},{text:"价税合计金额"},{text:"日期"},{text:"&nbsp;"}];
	list.push(line);
	line = [{text:"增值税应纳税额抵减税额信息",col:8,style:"title"}];
	list.push(line);
	line = [{text:"可抵减税额",col:3},{text:"本期申报抵减税额",row:2,col:2},{text:"本期未抵减税额",row:2,col:3}];
	list.push(line);
	line = [{text:"本期应抵减税额"},{text:"上期未抵减税额"},{text:"小计"}];
	list.push(line);
	line = [{widget:thisAllowCredit}, {widget:preLeftCredit}, {widget:totalAllowCredit}, {widget:applyVatCredit,col:2}, {widget:thisLeftCredit,col:3}];
	list.push(line);
	
	var table = createTable(list);
	table.className = "submit_form_table";
	table.align = "center";
	table.cellSpacing = "0";
	table.cellPadding = "5";
	
	var sepInvoices = [];
	var manInvoices = [];
	var fixInvoices = [];
	var othInvoices = [];
	if (credit != null && credit.invoices != null)
	{
		for (var i = 0; i < credit.invoices.length; i++)
		{
			var inv = credit.invoices[i];
			
			if (inv.invoiceCategory == SEP)
				sepInvoices.push(inv);
			else if (inv.invoiceCategory == MAN)
				manInvoices.push(inv);
			else if (inv.invoiceCategory == FIX)
				fixInvoices.push(inv);
			else if (inv.invoiceCategory == OTH)
				othInvoices.push(inv);
		}
	}
	
	createSepInvoice(table, sepInvoices);
	createNorInvoice(table, manInvoices, fixInvoices);
	createOthInvoice(table, othInvoices);
	
	var container = $("table_content");
	container.appendChild(table);
	
	if (credit != null) left();
}